package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private RedisTemplate<Object ,Object> redisTemplate;

    @Override
    public OverviewVo getOverview() {

        //1.访问redis查询数据
        log.info("从Reids中,查询线索与商机的概览数据");
        Object overview = redisTemplate.opsForValue().get("OVERVIEW");
        if (overview != null){
            log.info("从Reids中,查询线索与商机的概览数据,并返回");
            return (OverviewVo) overview;
        }
        //2.获取线索对应的数据
        OverviewVo clueOverview = clueMapper.getClueOverview();
        //2.1.获取商机对应数据
        OverviewVo businessOverview = businessMapper.getBusinessOverview();
        //2.2.封装Vo数据
        BeanUtils.copyProperties(businessOverview,clueOverview,"clueTotal","clueWaitAllot","clueWaitFollow","clueFollowing","clueFalse","clueConvertBusiness");

        //3.保存数据到redis中
        log.info("将线索与商机的概览数据保存到Reids中");
        redisTemplate.opsForValue().set("OVERVIEW" ,clueOverview ,5 , TimeUnit.MINUTES);

        return clueOverview;
    }
}
